#!/usr/bin/env python3
from config import *
import pymongo
import pandas as pd
from multiprocessing import Pool
import os

#配置mongodb的信息
client = pymongo.MongoClient(MONGO_URL)
db_paper_info = client[MONGO_PAPER_INFO_DB]
db_field_info = client[MONGO_RESEARCHER_field_DB]

'''
读取paper_info数据库中的数据
'''
def get_db_data(university_name):
    university_info_table = db_paper_info[university_name]
    university_data = pd.DataFrame(list(university_info_table.find()))
    return university_data

'''
获取到university_name中的所有的field值
'''
def get_all_field_info(university_name):
    university_data = get_db_data(university_name)

    field_list = []
    i = 0
    for data_item in university_data.values:
        print(type(data_item[4]))
        if type(data_item[4]) == list:
            for field_item in data_item[4]:
                # print(type(field_item))
                field_list.append(field_item.get("FN"))

    print(university_name + "已完成载入数据")
    return list(set(field_list))
        # for field_item in data_item[4]:
        #     print(field_item)
        #print(data_item[4])
        #print(i)
        #print(len(university_data.values))
        #i += 1
        #for filed_item in data_item[4]:
        #    print(filed_item)

'''
将数据存入txt
'''
def save_into_txt(field_list):
    try:
        with open("field_list.txt",mode='a+',encoding='utf-8') as f:
            for field_item in field_list:
                f.write(field_item + '\n')
        f.close()
    except FileNotFoundError:
        os.mknod("field_list.txt")

'''
存入mongod
'''
def save_into_mongod(field_list):
    pass


def main():
    pool = Pool()
    field_list = []
    # field_list.append(pool.map(get_all_field_info,UNIVERSITY_LIST2))
    for university_name in UNIVERSITY_LIST2:
        field_list.append(get_all_field_info(university_name))
    # field_list = get_all_field_info(UNIVERSITY_LIST[0])
    print(len(field_list))
    after_field_list = []
    for field_item in field_list:
        for data_item in field_item:
            after_field_list.append(data_item)
    done_field_list = list(set(after_field_list))
    print("一共有" + str(len(done_field_list)) + "个小领域")
    save_into_txt(done_field_list)

if __name__ == '__main__':
    main()
